/*
 * @Author: Nick
 * @Date: 2021-04-28 17:18:50
 * @LastEditTime: 2021-05-12 16:16:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /emotech-wxapp-EF/src/api/index.js
 */
import Taro from '@tarojs/taro'
import request from '@/utils/request'
import { getUserProfile } from '@/utils/index'
import { miniProgramType } from '@/utils/config'
import { get as getGlobalData } from '@/utils/global_data'
/**
 * @description: 登录
 * @return {object} 用户信息
 */
export const wxLogin = async () => {
  const wxUser = await getUserProfile()
  if (wxUser.errMsg !== 'getUserProfile:ok') return
  const code = getGlobalData('code')
  console.log(code, 'code')
  const params = {
    url: '/api/wxAppLogin',
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      code: code,
      encryptedData: wxUser.encryptedData,
      iv: wxUser.iv,
      type: 1,
      miniProgramType: miniProgramType,
      isJoin: false,
    }
  }
  try {
    const result = await request(params)
    const newUser = { ...result, ...result.userVO }
    delete newUser.userVO
    Taro.setStorageSync('userInfo', newUser)
    return newUser
  } catch (error) {
    console.error(error)
  }
}

// 获取年龄分组
export const getAgeGroupList = async () => {
  try {
    const params = {
      url: '/api/sam/user/getAgeGroupList',
      method: 'GET',
    }
    const result = await request(params)
    return result
  } catch (error) {
    console.error(error)
  }
}

// 更新年龄
export const updateAgeGroup = async (code) => {
  try {
    await request({
      url: `/api/sam/user/updateAgeGroup?code=${code}`,
      method: 'POST'
    })
    return true
  } catch (error) {
    return false
  }
}

// 更新手机号
export const updatePhone = async (data) => {
  try {
    const params = {
      url: '/api/sam/user/updateMobile',
      method: 'POST',
      data: data,
    }
    const result = await request(params)
    return result
  } catch (error) {
    console.error(error)
  }
}

/** 更新位置信息 */
export async function updateLocation({ latitude = null, longitude = null, address = null }) {
  const response = await request({
    url: '/api/ef/user/updateLocation',
    data: {
      latitude,
      longitude,
      address: address
    },
    method: "POST"
  })
  return !!response
}

// 获取排行榜
export const getUserRanks = async ({ userId, pageNum, pageSize }) => {
  const params = {
    url: `/api/sam/task/getUserScoreRanks`,
    method: 'POST',
    data: {
      userId,
      pageNum,
      pageSize
    }
  }
  const res = await request(params)
  return res
}

/** 获取 task */
export const getUserTasks = async (id = '') => {
  const params = {
    url: '/api/sam/task/getTaskMe',
    method: 'POST',
    data: {
      userId: id
    }
  }
  return await request(params)
}

/** 获取用户二维码 */
export const getQCode = async () => {
  return await request({ url: `/api/sam/share/getWXShareCode?page=pages/landing/index` })
}

/** 获取海报 */
export const getUserPoster = async () => {
  return await request({ url: `/api/sam/share/getSharePoster` })
}

/** 获取用户荣耀详情 */
export const getUserProfilePage = async (userId) => {
  return await request({ url: '/api/profile/getUserProfilePage', data: { userId: userId } })
}

/** 获取当前 lesson 的 task 数据 */
export const getLessonById = async (lessonId) => {
  return await request({ url: '/api/task/getLessonById', data: { lessonId: lessonId } })
}

/** 练习结果上传给服务器 */
export const practiseVideoResult = async (options) => {
  return await request({
    url: '/api/score/practiseVideoResult',
    method: "POST",
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    data: {
      videoUrl: options.videoUrl,
      imgUrl: options.imgUrl,
      taskId: options.taskId,
      lessonId: options.lessonId,
      practiseId: options.practiseId,
      result: options.result,
      requiredTime: options.requiredTime,
      videoSize: options.videoSize,
      userId: options.userId
    }
  }
  )
}

/** 查看是否完成分享回流 */
export const hasUserShared = async () => {
  return await request({ url: '/api/sam/share/hasSharedAndBackFlow' })
}

/** 获取用户再哪个班级 */
export const getUserClass = async () => {
  return await request({ url: '/api/user/getMyClassName' })
}

/** 获取用户再哪个班级 */
export const getUserInfo = async (userId) => {
  return await request({ url: '/api/user/getInfoByUserId?userId=' + userId })
}

/** 用户保存海报 */
export const downloadPosterCount = async () => {
  return await request({ url: '/api/sam/share/downloadPosterCount' })
}

/** 获取战绩报告的分数等 */
export const getLessonScore = async (lessonId) => {
  return await request({ url: '/api/score/getLessonScore', data: { lessonId } })
}

/** 获取下一关lessonId */
export const getNextLessonId = async (lessonId) => {
  return await request({ url: `/api/sam/task/getNextLessonId?lessonId=${lessonId || ''}` })
}

/** 修改用户昵称 */
export const updateUserName = async (name) => {
  return await request({
    url: '/api/user/modifyUserName', method: 'POST', header: {
      'content-type': 'application/x-www-form-urlencoded'
    }, data: { name }
  })
}

/** 重新挑战 lesson */
export const retryLesson = async (lessonId, token) => {
  return await request({
    url: '/api/score/retryLesson?lessonId=' + lessonId, method: 'POST', header: {
      'content-type': 'application/x-www-form-urlencoded', 'token': token
    }
  })
}
